export const registerEvent = (lf) => {

	const { eventCenter } = lf.graphModel;

	eventCenter.on("node:mouseenter", (data) => {
		const { edges } = lf.getGraphRawData();
		let node = lf.getNodeModelById(data.data.id);
		//刚添加的节点,会展示hover信息
		if (node.properties.isDragging) {
			delete node.properties.isDragging;
			return;
		}
		showNodeHoverConfig(lf, data);

		if (edges) {
			openFromEdgeAnima(lf, edges, data.data.id);
		}
	});

	eventCenter.on("node:mouseleave", (data) => {
		const { edges } = lf.getGraphRawData();

		//设置悬浮按钮的属性
		hideNodeHoverConfig(lf, data)
		if (edges) {
			edges.forEach(({ id }) => {
				lf.closeEdgeAnimation(id);
			});
		}

	});

	eventCenter.on("edge:mouseenter", (data) => {
		const { edges } = lf.getGraphRawData();
		// console.log(edges)

		if (edges) {
			openToEdgeAnima(lf, edges, data.data.targetNodeId);
		}
		console.log("edge:mouseenter")
		showEdgeHoverConfig(lf, data);
	});

	eventCenter.on("edge:mouseleave", (data) => {

		const { edges } = lf.getGraphRawData();
		hideEdgeHoverConfig(lf, data);

		if (edges) {
			edges.forEach(({ id }) => {
				lf.closeEdgeAnimation(id);
			});
		}
	});

	eventCenter.on("node:dragstart", (data) => {
		console.log("dragstart")
		//设置悬浮按钮的属性
		hideNodeHoverConfig(lf, data);
	});


	eventCenter.on("node:drop", (data) => {
		//设置悬浮按钮的属性
		showNodeHoverConfig(lf, data);
		console.log("drop")
	});

	eventCenter.on("node:dnd-add", (data) => {
		//设置悬浮按钮的属性
		hideNodeHoverConfig(lf, data);
		console.log("add")
	});




	eventCenter.on("node:drag", (data) => {
		hideNodeHoverConfig(lf, data);
		console.log("drag")

	});


	eventCenter.on("node:add", (data) => {
		console.log("add")
	});

	eventCenter.on("node:dbclick", (data) => {
		let node = lf.getNodeModelById(data.data.id);
		console.log("node:dbclick")
		if (!node.isDrawView()) {
			return;
		}
		lf.setNodePropertiesConfig({
			properties: data.data.properties,
			id: data.data.id,
			type: data.data.type,
			showFlag: true
		})
	});

	eventCenter.on("selection:contextmenu", (data) => {

	});




}

const hideNodeHoverConfig = (lf, data) => {
	// if(1==1){
	// 	return;
	// }
	lf.setNodeHoverConfig({
		properties: data.data.properties,
		id: data.data.id,
		type: data.data.type,
		showFlag: false
	})
}


const showNodeHoverConfig = (lf, data) => {
	let vDom = null;
	if (data.e.target.classList.contains('baseNode')) {
		vDom = data.e.target
	} else if (data.e.target?.children[0]?.classList?.contains("baseNode")) {
		vDom = data.e.target.children[0];
	} else {
		vDom = lf.closest(data.e.target, ".baseNode");
	}

	if (!vDom) {
		return;
	}
	var boundingClientRect = vDom.getBoundingClientRect();

	//设置悬浮按钮的属性
	lf.setNodeHoverConfig({
		boundingClientRect,
		rect: {
			left: boundingClientRect.left,
			top: boundingClientRect.top,
			width: boundingClientRect.width,
			height: boundingClientRect.height,
		},
		id: data.data.id,
		type: data.data.type,
		properties: data.data.properties,
		showFlag: true
	})
}

const hideEdgeHoverConfig = (lf, data) => {
	lf.setEdgeHoverConfig({
		properties: data.data.properties,
		id: data.data.id,
		type: data.data.type,
		showFlag: false
	})
}

const showEdgeHoverConfig = (lf, data) => {
	let vDom = data.e.target;


	if (!vDom) {
		return;
	}

	var boundingClientRect = vDom.getBoundingClientRect();
	//设置悬浮按钮的属性
	lf.setEdgeHoverConfig({
		boundingClientRect,
		rect: {
			left: boundingClientRect.left,
			top: boundingClientRect.top,
			width: boundingClientRect.width,
			height: boundingClientRect.height,
		},
		id: data.data.id,
		type: data.data.type,
		properties: data.data.properties,
		showFlag: true
	})

}

const openFromEdgeAnima = (lf, edges, fromId) => {
	let targetEdges = [];
	edges.forEach((e) => {
		if (e.sourceNodeId == fromId) {
			lf.openEdgeAnimation(e.id);
			targetEdges.push(e.targetNodeId);
		}
	});
	if (targetEdges.length > 0) {
		for (const b of targetEdges) {
			openFromEdgeAnima(lf, edges, b);
		}
	}
};



const openToEdgeAnima = (lf, edges, toId) => {
	let targetEdges = [];
	edges.forEach((e) => {
		if (e.targetNodeId == toId) {
			lf.openEdgeAnimation(e.id);
			targetEdges.push(e.sourceNodeId);
		}
	});
	if (targetEdges.length > 0) {
		for (const b of targetEdges) {
			openToEdgeAnima(lf, edges, b);
		}
	}
};
